package com.example.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.dao.UserInfoDto;
import com.example.entity.UserInfo;
import com.example.exception.BaseException;
import com.example.mapper.UserInfoMapper;
import com.example.service.UserInfoService;
import com.example.vo.UserInfoVo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @CreateTime 2025-02-22
 * @Description 用户信息管理实现类
 */
@Service
@Slf4j
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo>
        implements UserInfoService {  // 查看泛型使用ctrl+p

    @Resource
    private UserInfoMapper userInfoMapper;

    /**
     * 根据id查询用户信息
     * @param id
     * @return
     */
    public UserInfoVo selectUserByIdResultMap(Long id) {
        UserInfoVo userInfoVo = new UserInfoVo();
        UserInfo userInfo = userInfoMapper.selectUserByIdResultMap(id);
        BeanUtils.copyProperties(userInfo,userInfoVo);
        return userInfoVo;
    }

    /**
     * 根据ids批量查询
     * @param ids
     * @return
     */
    public List<UserInfoVo> selectBatchByIds(List<Long> ids) {
        List<UserInfo> userInfos = userInfoMapper.selectByIds(ids);

        List<UserInfoVo> arrayList = new ArrayList<>();

        for (UserInfo userInfo: userInfos) {
            UserInfoVo userInfoVo = new UserInfoVo();
            BeanUtils.copyProperties(userInfo,userInfoVo);
            arrayList.add(userInfoVo);
        }

        return arrayList;
    }

    /**
     * 根据name查询
     * @param userInfo
     * @return
     */
    public UserInfo getByName(UserInfo userInfo) {
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("name",userInfo.getName());
        UserInfo user = userInfoMapper.selectOne(wrapper);
        return user;
    }

    /**
     * 批量、模糊查询
     * @param userInfoDto
     * @return
     */
    public List<UserInfoVo> queryBatch(UserInfoDto userInfoDto) {
        // <if test = "name" != null> and name like #{name} </if>
        List<UserInfo> userInfos = lambdaQuery()
                //.like(userInfoDto.getName() != null, UserInfo::getName, userInfoDto.getName())
                //.le(userInfoDto.getId() != null, UserInfo::getId, userInfoDto.getId())
                .list();

        List<UserInfoVo> arrayList = new ArrayList<>();

        for (UserInfo userInfo: userInfos) {
            UserInfoVo userInfoVo = new UserInfoVo();
            BeanUtils.copyProperties(userInfo,userInfoVo);
            arrayList.add(userInfoVo);
        }

        return arrayList;
    }

    /**
     * 插入前校验
     * @param userInfoList
     */
    public void getCheckBatch(List<UserInfo> userInfoList) {
        //提取用户名
        List<String> names = userInfoList.stream()
                .map(UserInfo::getName)
                .collect(Collectors.toList());

        if (!names.isEmpty()) {
            // 1. 构建查询条件：查询 name 字段在 names 列表中的记录
            LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<UserInfo>()
                    .in(UserInfo::getName, names)  // 字段名通过 Lambda 表达式指定
                    .select(UserInfo::getName);    // 可选：仅查询 name 字段（减少数据传输）

            // 2. 执行查询：获取数据库中已存在的用户列表
            List<UserInfo> existingUsers = userInfoMapper.selectList(queryWrapper);

            // 3. 记录日志：输出实际存在的用户名
            log.info("用户名列表：{}", existingUsers.stream()
                    .map(UserInfo::getName)
                    .collect(Collectors.toList()));

            // 4. 校验是否存在冲突
            if (!existingUsers.isEmpty()) {
                throw new BaseException("999", "用户" + existingUsers.stream().map(UserInfo::getName).collect(Collectors.toList()) +"已存在" );
            }
        }
    }
}
